import {defineStore} from 'pinia'
import {i18n} from "@/i18n";

const t = i18n.global.t;

export interface User {
  userAccount?: string
  username?: string
  password?: string
  role?: number
  userId?: number
  permissionList: number[]
}

export enum UserRoleEnums {
  ADMIN = 1,
  NORMAL = 2,
}
export const UserRoleNameMap = {
  [UserRoleEnums.ADMIN]: 'userInfoModal.role_admin',
  [UserRoleEnums.NORMAL]: 'userInfoModal.role_normal',
}
export const UserRoleOption = [
  { text: UserRoleNameMap[UserRoleEnums.ADMIN], value: UserRoleEnums.ADMIN },
  { text: UserRoleNameMap[UserRoleEnums.NORMAL], value: UserRoleEnums.NORMAL },
]

export const getRoleName=(v)=>{
    // @ts-ignore
  return t(UserRoleNameMap[v])
}

export const USER_DATA_KEY = 'DATA_KEY'

export const useStore = defineStore('store', () => {
  let userString = localStorage.getItem(USER_DATA_KEY)
  const user = reactive<User>(
    userString
      ? JSON.parse(userString)
      : {
          username: '', // 昵称
        }
  )

  const isLogin = ref(user.userId)

  const role = ref(user.role)

  const setUser = (options: User) => {
    Object.assign(user, options)
    localStorage.setItem(USER_DATA_KEY, JSON.stringify(user))
    isLogin.value=user.userId
  }

  const removeUser = () => {
    localStorage.removeItem(USER_DATA_KEY)
    isLogin.value = 0;
  }

  return { isLogin,role, user, setUser ,removeUser}
})
